package com.example.test.springdemo.config;

import com.example.test.springdemo.interceptor.JwtTokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ：吉金武
 * @ClassName InterceptorConfig
 * @date ：2022/1/19 - 23:00
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 *
 **/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JwtTokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/**");
    }

    /**
     * 解决前后端分离请求跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST","GET","PUT","DELETE")
                .allowCredentials(false)
                .maxAge(30);
    }
}

